package com.zrwl.poscloud.xo.service.impl;
import com.zrwl.poscloud.base.entity.CallProductOrder;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.entity.Sequence;
import com.zrwl.poscloud.base.enums.EBusiType;
import com.zrwl.poscloud.base.enums.EBusinessType;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.holder.RequestHolder;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.service.AdminService;
import com.zrwl.poscloud.xo.service.SequenceService;
import com.zrwl.poscloud.xo.vo.AdminVO;
import com.zrwl.poscloud.xo.vo.SequenceVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import com.zrwl.poscloud.xo.mapper.CallProductOrderMapper;
import com.zrwl.poscloud.xo.service.CallProductOrderService;
import com.zrwl.poscloud.xo.vo.CallProductOrderVO;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 通话产品订单 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2023-06-09
 */
@Service
public class CallProductOrderServiceImpl extends SuperServiceImpl<CallProductOrderMapper,CallProductOrder> implements CallProductOrderService {

    @Autowired
    private CallProductOrderService callProductOrderService;

    @Autowired
    private SequenceService sequenceService;

    @Autowired
    private AdminService adminService;

    @Resource
    private CallProductOrderMapper callProductOrderMapper;
    @Override
    public IPage<CallProductOrderVO> getPageList(CallProductOrderVO callProductOrderVO){
       Page<CallProductOrder> page = new Page<>();
       page.setCurrent(callProductOrderVO.getCurrentPage());
       page.setSize(callProductOrderVO.getPageSize());
       IPage<CallProductOrderVO> pageList = callProductOrderMapper.getPageList(page,callProductOrderVO);
       List<CallProductOrderVO> list = pageList.getRecords();
       pageList.setRecords(list);
       return pageList;
}
    @Override
    public List<CallProductOrderVO> getAllList(CallProductOrderVO callProductOrderVO){
        List<CallProductOrderVO> list = callProductOrderMapper.getOrderList(callProductOrderVO);
        return list;
}
    @Override
    public String add(CallProductOrderVO callProductOrderVO){
        CallProductOrder entity = new CallProductOrder();
        BeanUtils.copyProperties(callProductOrderVO, entity, SysConf.STATUS);
        entity.insert();
        callProductOrderVO.setUid(entity.getUid());
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);}
    @Override
    public String edit(CallProductOrderVO callProductOrderVO){
        CallProductOrder entity = callProductOrderService.getById(callProductOrderVO.getUid());
        BeanUtils.copyProperties(callProductOrderVO, entity);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
}
    @Override
    public String delete(CallProductOrderVO callProductOrderVO){
        CallProductOrder entity = callProductOrderService.getById(callProductOrderVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
}

    @Override
    public String getAutoOrderCode() {
        HttpServletRequest request = RequestHolder.getRequest();
        String ownerUid = null;
        Integer ownerType = null;
        if (request.getAttribute(SysConf.OWNER_UID) != null) {
            ownerUid = request.getAttribute(SysConf.OWNER_UID).toString();
            ownerType = (Integer) request.getAttribute(SysConf.OWNER_TYPE);
        }
        if (ownerType == null || ownerUid == null) {
            AdminVO adminVO = adminService.getMe();
            ownerType = adminVO.getOwnerType();
            ownerUid = adminVO.getCompanyUid();
        }
        //获取自动生成人员编号
        SequenceVO sequenceVO = new SequenceVO();
        sequenceVO.setOwnerType(ownerType);
        sequenceVO.setOwnerUid(ownerUid);
        sequenceVO.setBusiType(EBusiType.callOrderCode);
        sequenceVO.setCodeType(EBusinessType.employeeAutoCode);
        Sequence sequence = sequenceService.customizeSelect(sequenceVO);
        String code = sequenceService.generateSerialCode(sequence);
        return code;
    }
}
